package com.cloud.hotline.api.web;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.hotline.api.bean.HotlineCalculateForm;
import com.cloud.hotline.api.service.HotlineExportService;
import com.cloud.hotline.common.utils.DateHelper;

@RestController
@RequestMapping("hotline/api/export")
public class HotlineExportController {

	@Autowired
	private HotlineExportService hotlineExportService;

	@PostMapping("doc")
	public ResponseEntity<byte[]> calculate(@RequestBody HotlineCalculateForm form) throws Exception {
		HttpHeaders headers = new HttpHeaders();
		String fileName = new String(
				(DateHelper.dateToFormatStr(new Date(), "yyyyMMdd") + "东城区市民热线每日专报.doc").getBytes("ISO8859-1"),
				"UTF-8");
		headers.add("Content-Disposition", "attachment;filename=" + fileName);
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		return new ResponseEntity<byte[]>(hotlineExportService.export(form), headers, HttpStatus.OK);
	}
}
